﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace POC.Controls
{
    public class MessageBoxEx : WindowBase
    {
        static MessageBoxEx()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageBoxEx), new FrameworkPropertyMetadata(typeof(MessageBoxEx)));
        }

        private MessageBoxEx()
        {
            this.Height = 180;
            this.Width = 400;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.Loaded += MessageBoxEx_Loaded;
        }

        private void MessageBoxEx_Loaded(object sender, RoutedEventArgs e)
        {
            VisualHelper.FindVisualChildList<Button>(this).ForEach(p => p.Click += Button_Click);
            var txtMessage = VisualHelper.FindVisualChild<TextBlock>(this, "txtMessage");
            if (txtMessage != null && txtMessage.ActualHeight > 30)
            {
                this.Height = 30 + txtMessage.ActualHeight + 46 < this.Height ? this.Height : 30 + txtMessage.ActualHeight + 46;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            switch (btn.Content.ToString())
            {
                case "确认":
                    this.Result = MessageBoxResult.OK;
                    break;
                case "取消":
                    this.Result = MessageBoxResult.Cancel;
                    break;
                case "是":
                    this.Result = MessageBoxResult.Yes;
                    break;
                case "否":
                    this.Result = MessageBoxResult.No;
                    break;
            }
            this.Close();
        }

        public string MessageBoxText
        {
            get { return (string)GetValue(MessageBoxTextProperty); }
            set { SetValue(MessageBoxTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MessageBoxText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MessageBoxTextProperty =
            DependencyProperty.Register("MessageBoxText", typeof(string), typeof(MessageBoxEx));


        public string Caption
        {
            get { return (string)GetValue(CaptionProperty); }
            set { SetValue(CaptionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Caption.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CaptionProperty =
            DependencyProperty.Register("Caption", typeof(string), typeof(MessageBoxEx));

        public MessageBoxButton BoxButton
        {
            get { return (MessageBoxButton)GetValue(BoxButtonProperty); }
            set { SetValue(BoxButtonProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Button.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BoxButtonProperty =
            DependencyProperty.Register("BoxButton", typeof(MessageBoxButton), typeof(MessageBoxEx), new PropertyMetadata(MessageBoxButton.OK));


        public MessageBoxImage BoxImage
        {
            get { return (MessageBoxImage)GetValue(BoxImageProperty); }
            set { SetValue(BoxImageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Icon.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BoxImageProperty =
            DependencyProperty.Register("BoxImage", typeof(MessageBoxImage), typeof(MessageBoxEx), new PropertyMetadata(PropertyChangedCallback));


        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(MessageBoxEx));


        public static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var messagebox = d as MessageBoxEx;
            switch (messagebox.BoxImage)
            {
                case MessageBoxImage.Information:
                    messagebox.ImageSource = new BitmapImage(new Uri("../Images/Info.png", UriKind.RelativeOrAbsolute));
                    break;
                case MessageBoxImage.Error:
                    messagebox.ImageSource = new BitmapImage(new Uri("../Images/Error.png", UriKind.RelativeOrAbsolute));
                    break;
                case MessageBoxImage.Warning:
                    messagebox.ImageSource = new BitmapImage(new Uri("../Images/Warning.png", UriKind.RelativeOrAbsolute));
                    break;
                case MessageBoxImage.Question:
                    messagebox.ImageSource = new BitmapImage(new Uri("../Images/Question.png", UriKind.RelativeOrAbsolute));
                    break;
            }
        }

        public MessageBoxResult Result { get; set; }

        public static MessageBoxResult Show(string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.Information)
        {
            MessageBoxEx messageBox = new MessageBoxEx()
            {
                MessageBoxText = messageBoxText,
                Caption = caption,
                BoxButton = button,
                BoxImage = icon,
                WindowStartupLocation = WindowStartupLocation.CenterScreen
            };
            messageBox.ShowDialog();
            return messageBox.Result;
        }
    }
}
